package com.woniuxy.controller;


import com.woniuxy.conmon.enums.RedisConst;
import com.woniuxy.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



@RestController
@RequestMapping("api")
public class MinioController {

    @Autowired
    MinioUtils minioUtils;
    @Autowired
    RedisTemplate redisTemplate;

    @Value("${spring.minio.url}")
    String url;

    @Value("${spring.minio.bucket-name}")
    String bucketName;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadAvatar(@RequestParam MultipartFile file){

        String name = file.getOriginalFilename();
        String fileName = name.substring(0,5);

        minioUtils.upload(file,fileName);
        //图片上传成功把图片存到redis中
        redisTemplate.opsForSet().add(RedisConst.UPLOAD_IMAGES,fileName);

        String path = url+"/"+bucketName+"/"+fileName;
        System.out.println(path);
        return ResponseEntity.ok(path);
    }

    @DeleteMapping("/delete")
    public void deleteAvatar(@PathVariable String fileName){
        minioUtils.deleteImg(fileName);
    }

}
